<?php
namespace app\api\controller;
use think\Request;
use app\base\model\{
    Master as MasterModel,
    MasterVideo,
    MasterVideoGoods,
    Goods,
    LikeVideo,
    MasterCate
};

class Master extends Base{
    //大师模型
    protected $master;
    //大师视频模型
    protected $master_video;
    //大师视频推荐商品模型
    protected $video_goods;
    //大师分类模型
    protected $master_cate;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->master  = new MasterModel();
        $this->master_video=new MasterVideo();
        $this->video_goods=new MasterVideoGoods();
        $this->master_cate =new MasterCate();
    }
    /**
     *根据视频id大师信息以及视频信息
     *return json
     */
  public function masterInfo(){
     $video_id = $this->request->post('video_id');
      if(empty($video_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $videoInfo=$this->master_video->where(['v_id'=>$video_id])->find();
      $m_id=$videoInfo['m_id'];
      $masterInfo=$this->master->where(['m_id'=>$m_id,'is_delete'=>0])->find();
      $goods_list=$this->video_goods->alias('vg')
                                    ->join('yc_goods g','vg.gid=g.goods_id','left')
                                    ->where(['vg.vid'=>$video_id,'g.goods_status'=>1])
                                    ->field('g.goods_id,g.goods_name,g.goods_desc,g.goods_thumb')
                                    ->select();
      $likeVideo=new LikeVideo();
      if(empty($this->userInfo)){
          $videoInfo['is_like']=0;
      }else{
          $where=[
              'user_id'  =>$this->user_id,
              'video_id' =>$video_id
          ];
          $count=$likeVideo->where($where)->count();
          if($count>0){
              $videoInfo['is_like']=1;
          }else{
              $videoInfo['is_like']=0;
          }
      }
      $msg['message']=checkMsg(GET_DATA_SUCCESS);
      $msg['master_info']   =$masterInfo;
      $msg['videoInfo']=$videoInfo;
      $msg['goods_list']=$goods_list;
      return $msg;
  }
  /**
   *大师视频列表
   * return json
   */
  public function masterVideoList(){
      $video_id=$this->request->post('video_id');
      if(empty($video_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $videoInfo=$this->master_video->where(['v_id'=>$video_id])->find();
      $m_id=$videoInfo['m_id'];
      $list=$this->master_video->where(['m_id'=>$m_id,'status'=>1,'is_delete'=>0,'v_id'=>["<>",$video_id]])->order('v_id','desc')->select();
      if(!empty($list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['video_list']=$list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *更多大师
   * return json
   */
  public function moreMaster(){
      $video_id=$this->request->post('video_id');
      if(empty($video_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $videoInfo=$this->master_video->where(['v_id'=>$video_id])->find();
      $m_id=$videoInfo['m_id'];
      $list=$this->master->where(['m_id'=>['<>',$m_id],'is_delete'=>0])->order('m_id','desc')->select();
      if(!empty($list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['master_list']=$list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *收藏视频
   * return json
   */
  public function collectVideo(){
      if(empty($this->userInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $video_id = $this->request->post('video_id');
      if(empty($video_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $likeVideo = new LikeVideo();
      $where=[
          'user_id'  =>$this->user_id,
          'video_id' =>$video_id
      ];
      $count=$likeVideo->where($where)->count();
      if($count<1){
          $data=[
              'user_id' =>$this->user_id,
              'video_id'=>$video_id,
              'add_time'=>time()
          ];
          $res=$likeVideo->insertGetId($data);
          $msg['message']=checkMsg(OPERA_SUCCESS);
          $msg['is_like']=1;
      }else{
          $msg['message']=checkMsg(OPERA_FAIL);
      }
      return $msg;
  }
  /**
   *大师分类列表
   * return json
   */
 public function getMasterCate(){
     $cate_list = $this->master_cate->where(['cate_status'=>1])->select();
     if(!empty($cate_list)){
         $msg['message']=checkMsg(GET_DATA_SUCCESS);
         $msg['list'] = $cate_list;
     }else{
         $msg['message']=checkMsg(GET_DATA_FAILE);
     }
     return $msg;
 }
 /**
  *根据大师分类获取大师列表
  * return json
  */
 public function getMasterListByCate(){
     $cate_id = $this->request->post('cate_id',1);
     $master_list = $this->master->where(['is_delete'=>0,'cate_id'=>$cate_id])->select();
     if(!empty($master_list)){
         foreach($master_list as $key=>$value){
             $master_list[$key]['key']=$key;
         }
         $msg['message']=checkMsg(GET_DATA_SUCCESS);
         $msg['list']=$master_list;
     }else{
         $msg['message']=checkMsg(GET_DATA_FAILE);
     }
     return $msg;
 }


}